package com.pucong.gitee.database.backup.business.database.service;

import com.pucong.gitee.database.backup.common.entity.database.DatabaseTable;
import com.pucong.gitee.database.backup.business.database.util.OperationService;
import com.pucong.gitee.database.backup.business.database.util.OperationServiceOracleImpl;
import com.pucong.gitee.database.backup.business.database.util.OperationServicePgsqlImpl;
import com.pucong.gitee.database.backup.business.database.vo.DatabaseVo;
import com.pucong.gitee.database.backup.business.database.vo.DeleteDataTable;
import com.pucong.gitee.database.backup.business.database.vo.RenameDataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class OperationDataTableServiceImpl implements OperationDataTableService {

    @Autowired
    private DatabaseStaticService databaseStaticService;

    @Override
    public List<DatabaseTable> getTableList(String databaseId, DatabaseVo databaseVo) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sessionId = request.getSession().getId();

        OperationService operationService = databaseStaticService.getConnectionById(sessionId, databaseId);
        List<DatabaseTable> schemaList = null;
        if(operationService instanceof OperationServicePgsqlImpl || operationService instanceof OperationServiceOracleImpl){
            schemaList = operationService.getTableListByDBInfo(databaseVo.getSchemaName());
        }else{
            schemaList = operationService.getTableListByDBInfo(databaseVo.getDatabaseName());
        }
        return schemaList;
    }

    @Override
    public Boolean renameDataTable(String databaseId, RenameDataTable renameDataTable) {
        return null;
    }

    @Override
    public Boolean deleteDataTable(String databaseId, DeleteDataTable deleteDataTable) {
        return null;
    }
}
